﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_201_03
    {
        public static void ISYC_201_03(Dictionary<string, Object> data) //方形刀片和菱形刀片
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            double IC = 10;
            double L = IC;
            double S = 3;
            double AN = 7;
            double EPSR = 90;
            double EPSR1 = 75;
            double REN = 0.5;
            double RE = 2;
            double scale = IC / 10;//放大系数

            /*double IC = Convert.ToDouble(data["ic"]);
            double L = Convert.ToDouble(data["l"]);
            double S = Convert.ToDouble(data["s"]);
            double AN = Convert.ToDouble(data["an"]);
            double EPSR = Convert.ToDouble(data["epsr"]);
            double EPSR1 = Convert.ToDouble(data["epsr1"]);
            double REN = Convert.ToDouble(data["ren"]);
            double RE = Convert.ToDouble(data["re"]);
            double scale = IC / 10;//放大系数*/

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //方形
            //轮廓
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = fun.Polar(v1, ((360 - EPSR * 2) / 2.0) * pi / 180, L / 2.0);
            Vector2 v3 = fun.Polar(v1, ((360 - EPSR * 2) / 2.0) * pi / 180, L);
            Vector2 v4 = fun.Polar(v3, 0 * pi / 180, L);
            Vector2 v5 = fun.Polar(v1, 0 * pi / 180, L);
            Vector2 v6 = fun.Inters(v1, fun.Polar(v1, ((360 - EPSR * 2) / 4.0) * pi / 180, L), v3, fun.Polar(v3, -(EPSR / 2.0) * pi / 180, L));
            Vector2 v7 = new Vector2(v5.X + L, v5.Y);
            Vector2 v8 = fun.Polar(v7, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 v10 = new Vector2(v7.X, v7.Y + L);
            Vector2 v9 = fun.Inters(v8, fun.Polar(v8, 90 * pi / 180, L), v10, fun.Polar(v10, -(180 - AN) * pi / 180, L));
            Vector2 v11 = new Vector2(v7.X, v6.Y);

            //菱形
            Vector2 p1 = new Vector2(v1.X + L * 2, v1.Y);
            Vector2 p2 = fun.Polar(p1, EPSR1 * pi / 180, L / 2.0);
            Vector2 p3 = fun.Polar(p1, EPSR1 * pi / 180, L);
            Vector2 p4 = fun.Polar(p3, 0 * pi / 180, L);
            Vector2 p5 = fun.Polar(p1, 0 * pi / 180, L);
            Vector2 p6 = fun.Inters(p1, fun.Polar(p1, (EPSR1 / 2.0) * pi / 180, L), p3, fun.Polar(p3, -((360 - (EPSR1 * 2)) / 4.0) * pi / 180, L));
            Vector2 p7 = new Vector2(p5.X + L * 2, p5.Y);
            Vector2 p8 = fun.Polar(p7, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 p10 = new Vector2(p7.X, p7.Y + IC);
            Vector2 p9 = fun.Inters(p8, fun.Polar(p8, 90 * pi / 180, L), p10, fun.Polar(p10, -(180 - AN) * pi / 180, L));
            Vector2 p11 = new Vector2(p7.X, p6.Y);

            //标注点（虚线）
            Vector2 ic1 = fun.getPointForArcandLine(p6, fun.Polar(p6, 180 * pi / 180, L), p6, new Arc(p6, IC / 2.0, 0, 360));
            Vector2 ic2 = fun.getPointForArcandLine(p6, fun.Polar(p6, 0 * pi / 180, L), p6, new Arc(p6, IC / 2.0, 0, 360));

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));
            fun.PolyFill(dxf, polyline1, 0.1, contour);

            //倒角
            var arc1 = Pus.Fillet4LwPline(polyline1, 1, RE);
            var arc2 = Pus.Fillet4LwPline(arc1, 3, RE);
            var arc3 = Pus.Fillet4LwPline(arc2, 5, RE);
            var arc4 = Pus.Fillet4LwPline(arc3, 7, RE);
            fun.PolyFill(dxf, arc4, 0.3, contour);

            //虚线
            Circle circle1 = new Circle(v6, IC / 2.0);
            circle1.Layer = refin;
            circle1.LinetypeScale = 5;
            circle1.Linetype = Linetype.Dashed;
            dxf.AddEntity(circle1);

            //菱形
            //轮廓
            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(p2));
            polyline2.Vertexes.Add(new LwPolylineVertex(p3));
            polyline2.Vertexes.Add(new LwPolylineVertex(p4));
            polyline2.Vertexes.Add(new LwPolylineVertex(p5));
            polyline2.Vertexes.Add(new LwPolylineVertex(p1));
            polyline2.Vertexes.Add(new LwPolylineVertex(p2));
            fun.PolyFill(dxf, polyline2, 0.1, contour);

            //倒角
            var arc1_1 = Pus.Fillet4LwPline(polyline2, 1, REN);
            var arc2_1 = Pus.Fillet4LwPline(arc1_1, 3, RE);
            var arc3_1 = Pus.Fillet4LwPline(arc2_1, 5, REN);
            var arc4_1 = Pus.Fillet4LwPline(arc3_1, 7, RE);
            fun.PolyFill(dxf, arc4_1, 0.3, contour);

            //虚线
            Circle circle2 = new Circle(p6, IC / 2.0);
            circle2.Layer = refin;
            circle2.LinetypeScale = 5;
            circle2.Linetype = Linetype.Dashed;
            dxf.AddEntity(circle2);

            //标注---------------------------------------------------------------------------
            double height = 1 * scale;
            double arrowsize = 0.5 * scale;
            double exline = 0.5 * scale;

            //L,IC
            fun.dimrot1(dxf, v1, v5, 180, L * 0.4, 0.1, height, arrowsize, exline, "L=IC", "", "");

            //角度标注
            //EPSR
            fun.jdbz2(dxf, new Line(v3, fun.Polar(v3, ((360 - EPSR * 2) / 2.0) * pi / 180, 0.1)), new Line(v3, fun.Polar(v3, 180 * pi / 180, 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            //L（菱形）
            fun.dimrot1(dxf, p1, p5, 180, L * 0.4, 0.1, height, arrowsize, exline, "L", "", "");

            //IC（菱形）
            fun.dimrot1(dxf, ic1, ic2, 0, L * 0.8, 0.1, height, arrowsize, exline, "IC", "", "");

            //EPSR（菱形）
            fun.jdbz2(dxf, new Line(p4, fun.Polar(p4, 0 * pi / 180, 0.1)), new Line(p4, fun.Polar(p4, EPSR1 * pi / 180, 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
